import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

import static java.lang.System.out;

public class PichubServer {
	public static HashMap<String,Thread> clientList;
	ServerSocket myServer;
	ServerSocket picServer;
	public static Connection sqlCon = null;
	 String url = "jdbc:mysql://localhost:2046/";
	 String dbName = "pichubdb";
	  String driver = "com.mysql.jdbc.Driver";
	  String userName = "root";
	  String password = "1234";

	public PichubServer(){
		InetAddress thisIP;
		try{
			myServer = new ServerSocket(4546);
			picServer = new ServerSocket(4547);
			thisIP = InetAddress.getLocalHost();
			System.out.println("SERVER IP: " + thisIP.getHostAddress());
		}catch(IOException e){
			System.err.println(e);
		}

		 System.out.println("PICHUB SERVER STARTED");


		 connectToSQL();
	//	 retrieveImage(1);
		// saveImage("0.jpg");
		 listen();
	}

	public PichubServer(int port,int port2){
		try{
			myServer = new ServerSocket(port);
			picServer = new ServerSocket(port2);
		}catch(IOException e){
		}
		 System.out.println("Server Started...");

		 connectToSQL();
		 listen();
	}

	private void connectToSQL(){
		 try {
		  Class.forName(driver).newInstance();
		  sqlCon = DriverManager.getConnection(url+dbName,userName,password);
		  System.out.println("CONNECTED TO DATABASE pichubdb");
		  //sqlCon.close();
		  //System.out.println("DISCONNECTED FROM DATABASE pichubdb");
		 }catch(Exception e){
			 System.err.println("MYSQL:"+e);
		 }

	}

	  private void saveImage(String imageName){
	    	try{
	    		File myImage = new File(imageName);
	    		FileInputStream myImageStream = new FileInputStream( myImage );
	    		PreparedStatement myPrepareStatement = sqlCon.prepareStatement("INSERT INTO image(imageData) VALUES (?)");
	    		myPrepareStatement.setBinaryStream(1, (InputStream)myImageStream, (int)(myImage.length()));
	    		myPrepareStatement.executeUpdate();
	    	}catch(Exception e){
	    		System.err.println(e);
	    		System.err.println("Image Error");
	    	}
	    }

	private void listen(){
		 new PicClientHandler(picServer);
		 while(true) {
			 Socket newClient;
			 try{
			  if(myServer==null) System.out.println("Error");
			  newClient = myServer.accept();
			//  System.err.println("A new client");
			  System.out.println("A new client");
			  ClientThread myClients = new ClientThread(newClient);
			 // myClients.sendImage();
		     // String value = myClients.getIp();
		      synchronized(this)
		      {
		    	//  clientList.put(value, myClients);
		      }
			 }catch( IOException e ){
				 System.err.println(e);
		   }
		}  // end of while
	}

	/*
	private void retrieveImage(int id){
    //	Blob image = null;
    	ResultSet rs = null;
    	BufferedImage buffer = null;
    	try{
    		Statement myStatement = PichubServer.sqlCon.createStatement();
    		rs = myStatement.executeQuery("SELECT imageData From image WHERE id = '" + id +"'");
    		if(rs.next()){
    			InputStream img = rs.getBinaryStream(1);
    			 File outputfile = new File("saved.jpg");
    			 System.err.println("Here");
    			 buffer = ImageIO.read(img);
    			 ImageIO.write(buffer, "jpg", outputfile);
    			//image = rs.getBlob(1);
    			 System.err.println(outputfile.getAbsolutePath());
    		}
    	}catch(Exception e){
    		System.err.println("Image Error");
    		System.err.println(e);
    	}

    //	return image;
    }
	*/

	public static void main(String[] args){
		if( args.length>0 ){
			new PichubServer(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
		}
		new PichubServer();
	}
}
